import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/1/4.
 */
public class E20_CachedThreadPool {

    public static void main(String[] args) throws Exception{
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            exec.execute(new LiftOff());
        }
        TimeUnit.SECONDS.sleep(1);
        exec.shutdownNow();
    }
}

class LiftOff implements Runnable{
    protected int countDown = 50000;
    private static int taskCount = 0;
    private final int id = taskCount++;
    public LiftOff(){
    }
    public LiftOff(int countDown){
        this.countDown  =countDown;
    }
    public String status(){
        return "#" + id + "(" +
                (countDown > 0 ? countDown : "Liftoff!") + "), ";
    }
    @Override
    public void run() {
        while(countDown-->0){
            if (Thread.currentThread().isInterrupted()){
                System.out.println("Interrupted:#"+id);
                return;
            }
            System.out.print(status());
            Thread.yield();
        }
    }

}